package org.example.java.util.colls;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * 2015/10/16 10:28
 *
 * @author juntao zhang
 */
public class LinkedHashMapInsertTest {
  public static void main(String[] args) {
    final int cacheSize = 5;
    LinkedHashMap<String, Long> lru = new LinkedHashMap<String, Long>((int) Math.ceil(cacheSize / 0.75f) + 1, 0.75f, false) {//插入顺序
      @Override
      protected boolean removeEldestEntry(Map.Entry<String, Long> eldest) {
        System.out.println("size=" + size());
        return size() > cacheSize;
      }
    };
    lru.put("1", System.nanoTime());
    lru.put("2", System.nanoTime());
    lru.put("3", System.nanoTime());
    System.out.println(lru.get("1"));
    lru.put("4", System.nanoTime());
    lru.put("5", System.nanoTime());
    lru.put("6", System.nanoTime());//已经删除1
    System.out.println(lru.get("2"));
    System.out.println(lru.get("1"));//null
    System.out.println(lru.get("6"));

  }
}
